import type { MypageData, ReLogin } from '@/types/mypage'
import type { CartData } from '../types/cart'
import store from '../store/index'

export const getToken = (payload: string): {
    type: 'user/setToken', payload: string
} => {
    return { type: 'user/setToken', payload }
}

export const getUserMessage = (payload: MypageData): {
    type: 'user/setUser', payload: MypageData
} => {
    return { type: 'user/setUser', payload }
}

export const cleUserMessage = (payload: any): {
    type: 'user/cleToken', payload: any
} => {
    return { type: 'user/cleToken', payload }
}

//购物车仓库
// {img,number,price,proid,proname,state}:CartData
export const AddTocart = (cartlist: CartData): {
    type: 'cart/addGoods', payload: any
} => {
    let cartList = store.getState().cart
    const oldCartList1 = cartList.find(item => item.proid === cartlist.proid)
    if (oldCartList1) {
        oldCartList1.number += cartlist.number
    } else {
        // state.push(action.payload)
        cartList = [...cartList, { ...cartlist }]
    }
    return { type: 'cart/addGoods', payload: cartList }
}

export const DeFromcart = (proid:string): {
    type: 'cart/delGoods', payload: any
} => {
    const cartList = store.getState().cart
    const newCaetList = cartList.filter(item=>item.proid!==proid)
    return { type: 'cart/delGoods', payload:[...newCaetList]}
}

export const ChanNumcart = ({ proid, number }: { proid: string, number: number }): {
    type: 'cart/chgeGoods', payload: any
} => {
    const cartList = store.getState().cart
    const oldCartList = cartList.find(item => item.proid === proid)
    if (oldCartList) {
        oldCartList.number = number
    }
    return { type: 'cart/chgeGoods', payload: cartList }
}
//其他
export const ChanState = (payload: { proid: string, e: boolean }): {
    type: 'cart/comGoods', payload: any
} => {
    const cartList = store.getState().cart
    const oldCartList = cartList.find(item => item.proid === payload.proid)
    if (oldCartList) {
        oldCartList.state = payload.e
    }
    return { type: 'cart/comGoods', payload: cartList }
}

export const ChangeAllcheck = (state: boolean): {
    type: 'cart/isAllCheck', payload: any
} => {
    const cartList = store.getState().cart
    cartList.forEach(item => {
        item.state = state
    })
    return { type: 'cart/isAllCheck', payload: cartList }
}


